package com.shd.edanpei.view.picker;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.shd.edanpei.R;

import java.util.List;


/**
 * 列表选择器
 */
public class ListPicker {

    /**
     * 定义结果回调接口
     */
    public interface ResultHandler {
        void handle(String data);
    }

    public enum SCROLL_TYPE {
        HOUR(1),
        MINUTE(2);

        SCROLL_TYPE(int value) {
            this.value = value;
        }

        public int value;
    }

    private int scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
    private ResultHandler handler;
    private Context context;
    private boolean canAccess = false;

    private Dialog listPickerDialog;
    private PickerView pickerView;


    private List<String> stringList;
    private String title;
    private TextView tv_cancle, tv_title, tv_select;
    private String mData;

    public ListPicker(Context context, String title, List<String> list, ResultHandler resultHandler) {
        canAccess = true;
        this.context = context;
        this.handler = resultHandler;
        this.title = title;
        this.stringList = list;
        initDialog();
        initView();
    }

    private void initDialog() {
        if (listPickerDialog == null) {
            listPickerDialog = new Dialog(context, R.style.time_dialog);
            listPickerDialog.setCancelable(false);
            listPickerDialog.setCanceledOnTouchOutside(true);
            listPickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            listPickerDialog.setContentView(R.layout.picker_list);
            Window window = listPickerDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = dm.widthPixels;
            window.setAttributes(lp);
        }
    }

    private void initView() {
        pickerView = listPickerDialog.findViewById(R.id.picker_view);
        tv_cancle = listPickerDialog.findViewById(R.id.tv_cancel);
        tv_title = listPickerDialog.findViewById(R.id.tv_title);
        tv_select = listPickerDialog.findViewById(R.id.tv_select);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listPickerDialog.dismiss();
            }
        });

        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(mData)) {
                    handler.handle(mData);
                }
                listPickerDialog.dismiss();
            }
        });
    }

    private void initTimer() {


    }


    private void loadComponent() {
        pickerView.setData(stringList);
        if (stringList != null && stringList.size() > 0) {
            mData = stringList.get(0);
        }
        tv_title.setText(title);
        pickerView.setSelected(0);
        executeScroll();
    }

    private void addListener() {
        pickerView.setOnSelectListener(new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                mData = text;
            }
        });

    }

    private void executeScroll() {
        pickerView.setCanScroll(stringList.size() > 1);
    }

    private int disScrollUnit(SCROLL_TYPE... scroll_types) {
        if (scroll_types == null || scroll_types.length == 0) {
            scrollUnits = SCROLL_TYPE.HOUR.value + SCROLL_TYPE.MINUTE.value;
        } else {
            for (SCROLL_TYPE scroll_type : scroll_types) {
                scrollUnits ^= scroll_type.value;
            }
        }
        return scrollUnits;
    }

    public void show() {
        if (canAccess) {
            canAccess = true;
            loadComponent();
            addListener();
//            setSelectedTime(time);
            listPickerDialog.show();
        }
    }

    /**
     * 设置日期控件是否可以循环滚动
     */
    public void setIsLoop(boolean isLoop) {
        if (canAccess) {
            this.pickerView.setIsLoop(isLoop);
        }
    }

    /**
     * 设置日期控件默认选中的时间
     */
    public void setSelectedTime(String time) {

    }

}
